#!/bin/bash
#
#cal some project someone edit lines
#
#
#********************某个项目最近24小时增加减少的代码量********************
#
#
# project_path='/root/Desktop/jenkins_private'
# cal_date=`date +%Y-%m-%d`
# long_time="24 hours ago"
# file_path="/var/log/git_cal.log"
# #
# cd ${project_path}
# #
# user_list=`git log --after="${long_time}" --pretty=format:%an |uniq`
# #
# user_code_lines()
# {
# for user in ${user_list};do
#     insert_lines=`git log --author=${user} -p --after="${long_time}" | grep  ^+ | grep -v "+++"| wc -l`
#     delete_lines=`git log --author=${user} -p --after="${long_time}" | grep -E ^- | grep -v "\\---"| wc -l`
#     echo -e "${cal_date}\t${user}\t${insert_lines}\t${delete_lines}" >> $file_path
#     mysql -u root -pqwe123 -D test <<EOF
# 	insert into git (insert_date,user,add_lines,del_lines) values ("${cal_date}","${user}","${insert_lines}","${delete_lines}")
# EOF
# done
# }
# user_code_lines
#
#
#
#************最近24小时每次提交的代码量**********************
#**********时间|版本号|用户|新增|删除|注释*******************
#
project_path="$1"   					  #项目路径
m_date=`date +%Y-%m-%d -d "1 days ago"`   #统计的日期
cal_time="5 days age"					  #统计时长
log_path="/tmp/git.log"
#
cd $project_path
project_name=`basename $PWD`
git pull
#输入用户名/SSH key
lines=`git log --stat --since="$cal_time" | grep -n ^commit | awk 'BEGIN{FS=":| " }{print $1}'`
lines=($lines)
commits_num=${#lines[*]}  			 	 #提交的次数
num=0
if [[ $commits_num == 0 ]];then
	echo "没有提交"
	exit 11
fi

for con in `seq 1 ${commits_num}`;do
	if [[ $con == $commits_num ]];then
		version_num=`git log --stat --since="$cal_time" | sed -n ${lines[$num]},"$"p | head -1 | cut -d " " -f2`
		author_user=`git log --stat --since="$cal_time" | sed -n ${lines[$num]},"$"p | head -2 | tail -1 | cut -d " " -f 2`
		commit_date=`git log --stat --since="$cal_time" | sed -n ${lines[$num]},"$"p | head -3 | tail -1 | awk 'BEGIN{FS=" "}{print $6,$3,$4,$5}'`
		author_mail=`git log --stat --since="$cal_time" | sed -n ${lines[$num]},"$"p | head -2 | tail -1 | awk 'BEGIN{FS="<|>"}{print $2}'`
		change_lines=`git log --stat --since="$cal_time" | tail -1 |  awk '{print $1,$4,$6}'`
		change_lines=($change_lines)
		edit_file_num=${change_lines[0]}
		add_lines_num=${change_lines[1]}
		del_lines_num=${change_lines[2]}
		echo -e "${commit_date}\t${project_name}\t${version_num}\t${author_user}\t${author_mail}\t${edit_file_num}\t${add_lines_num}\t${del_lines_num}" >> $log_path
	else
		version_num=`git log --stat --since="$cal_time" | sed -n ${lines[$num]},${lines[$con]}p | head -1 | cut -d " " -f2`
		author_user=`git log --stat --since="$cal_time" | sed -n ${lines[$num]},${lines[$con]}p | head -2 | tail -1 | cut -d " " -f 2`
		commit_date=`git log --stat --since="$cal_time" | sed -n ${lines[$num]},${lines[$con]}p | head -3 | tail -1 | awk 'BEGIN{FS=" "}{print $6,$3,$4,$5}'`
		author_mail=`git log --stat --since="$cal_time" | sed -n ${lines[$num]},${lines[$con]}p | head -2 | tail -1 | awk 'BEGIN{FS="<|>"}{print $2}'`
		change_lines=`git log --stat --since="$cal_time" | sed -n ${lines[$num]},${lines[$con]}p | tail -3 | head -1 | awk '{print $1,$4,$6}'`
		change_lines=($change_lines)
		edit_file_num=${change_lines[0]}
		add_lines_num=${change_lines[1]}
		del_lines_num=${change_lines[2]}
		echo -e "${commit_date}\t${project_name}\t${version_num}\t${author_user}\t${author_mail}\t${edit_file_num}\t${add_lines_num}\t${del_lines_num}" >> $log_path
#				 提交时间			项目名称		版本号			提交用户		用户邮箱		文件变化数		增加行数			删除行数
	fi
	num=$((${num}+1))
done

#	echo -e "${m_date}\t${version_num}\t${author_user}\t${author_mail}\t${edit_file_num}\t${add_lines_num}\t${del_lines_num}" >> $log_path